


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > Stack</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/idea.min.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.min.js"></script>
  <script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">com.it.alg</a>
</div>

<h1>Coverage Summary for Class: Stack (com.it.alg)</h1>

<table class="coverageStats">

<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">Stack</td>
<td class="coverageStat">
  <span class="percent">
    91.7%
  </span>
  <span class="absValue">
    (11/12)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    95.3%
  </span>
  <span class="absValue">
    (61/64)
  </span>
</td>
</tr>
  <tr>
    <td class="name">Stack$Itr</td>
<td class="coverageStat">
  <span class="percent">
    66.7%
  </span>
  <span class="absValue">
    (4/6)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    47.6%
  </span>
  <span class="absValue">
    (10/21)
  </span>
</td>
  </tr>
  <tr>
    <td class="name">Stack$Node</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (2/2)
  </span>
</td>
  </tr>
<tr>
  <td class="name"><strong>Total</strong></td>
<td class="coverageStat">
  <span class="percent">
    84.2%
  </span>
  <span class="absValue">
    (16/19)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    83.9%
  </span>
  <span class="absValue">
    (73/87)
  </span>
</td>
</tr>
</table>

<br/>
<br/>


<pre>
<code class="sourceCode" id="sourceCode">&nbsp;package com.it.alg;
&nbsp;
&nbsp;import java.util.ConcurrentModificationException;
&nbsp;import java.util.Iterator;
&nbsp;import java.util.NoSuchElementException;
&nbsp;import java.util.Objects;
&nbsp;import java.util.function.Consumer;
&nbsp;
&nbsp;import com.it.io.IOSequence;
&nbsp;
&nbsp;public class Stack&lt;S, E&gt; implements Iterable&lt;E&gt; {
&nbsp;
<b class="fc">&nbsp;	class Node {</b>
&nbsp;		E value;
<b class="fc">&nbsp;		int repeatCount = 1;</b>
&nbsp;		Node prev;
&nbsp;		Node next;
&nbsp;	}
&nbsp;	
&nbsp;	final Node head;
&nbsp;	final Node tail;
&nbsp;	Node top;
&nbsp;	int size;
<b class="fc">&nbsp;	int modCount = 0;</b>
&nbsp;	
&nbsp;	IOSequence&lt;S, E&gt; ioSequence;
&nbsp;	
<b class="fc">&nbsp;	public Stack() {</b>
<b class="fc">&nbsp;		this.head = new Node();</b>
<b class="fc">&nbsp;		this.tail = new Node();</b>
<b class="fc">&nbsp;		this.head.next = this.tail;</b>
<b class="fc">&nbsp;		this.tail.prev = this.head;</b>
<b class="fc">&nbsp;		this.top = head;</b>
&nbsp;	}
&nbsp;	
&nbsp;	public NodeInfo&lt;E&gt; push(E value) {
<b class="fc">&nbsp;		modCount++;</b>
<b class="fc">&nbsp;		Node node = new Node();</b>
<b class="fc">&nbsp;		node.value = value;</b>
<b class="fc">&nbsp;		node.prev = top;</b>
<b class="fc">&nbsp;		node.next = top.next;</b>
<b class="fc">&nbsp;		top.next.prev = node;</b>
<b class="fc">&nbsp;		top.next = node;</b>
<b class="fc">&nbsp;		top = node;</b>
<b class="fc">&nbsp;		size++;</b>
<b class="fc">&nbsp;		setCount(top);</b>
<b class="fc">&nbsp;		return new NodeInfo&lt;E&gt;(top.value, top.repeatCount);</b>
&nbsp;	}
&nbsp;	
&nbsp;	public NodeInfo&lt;E&gt; push() {
<b class="fc">&nbsp;		if (top.next == tail) {</b>
<b class="nc">&nbsp;			throw new NoSuchElementException();</b>
&nbsp;		}
<b class="fc">&nbsp;		modCount++;</b>
<b class="fc">&nbsp;		top = top.next;</b>
<b class="fc">&nbsp;		setCount(top);</b>
<b class="fc">&nbsp;		return new NodeInfo&lt;E&gt;(top.value, top.repeatCount);</b>
&nbsp;	}
&nbsp;	
&nbsp;	public NodeInfo&lt;E&gt; pop() {
<b class="fc">&nbsp;		return remove(top);</b>
&nbsp;	}
&nbsp;	
&nbsp;	public NodeInfo&lt;E&gt; add(E value) {
<b class="fc">&nbsp;		modCount++;</b>
<b class="fc">&nbsp;		Node node = new Node();</b>
<b class="fc">&nbsp;		node.value = value;</b>
<b class="fc">&nbsp;		node.repeatCount = 1;</b>
<b class="fc">&nbsp;		node.prev = tail.prev;</b>
<b class="fc">&nbsp;		node.next = tail;</b>
<b class="fc">&nbsp;		tail.prev.next = node;</b>
<b class="fc">&nbsp;		tail.prev = node;</b>
<b class="fc">&nbsp;		size++;</b>
<b class="fc">&nbsp;		return new NodeInfo&lt;E&gt;(node.value, node.repeatCount);</b>
&nbsp;	}
&nbsp;	
&nbsp;	public NodeInfo&lt;E&gt; remove(Node node) {
<b class="fc">&nbsp;		if (node == head || node == tail) {</b>
<b class="nc">&nbsp;			throw new NoSuchElementException();</b>
&nbsp;		}
<b class="fc">&nbsp;		modCount++;</b>
<b class="fc">&nbsp;		Node prev = node.prev;</b>
<b class="fc">&nbsp;		Node next = node.next;</b>
<b class="fc">&nbsp;		prev.next = next;</b>
<b class="fc">&nbsp;		next.prev = prev;</b>
<b class="fc">&nbsp;		node.prev = null;</b>
<b class="fc">&nbsp;		node.next = null;</b>
<b class="fc">&nbsp;		if (top == node) {</b>
<b class="fc">&nbsp;			top = prev;</b>
&nbsp;		}
<b class="fc">&nbsp;		size--;</b>
<b class="fc">&nbsp;		return new NodeInfo&lt;E&gt;(node.value, node.repeatCount);</b>
&nbsp;	}
&nbsp;	
&nbsp;	public int size() {
<b class="fc">&nbsp;		return size;</b>
&nbsp;	}
&nbsp;	
&nbsp;	public boolean isAllPushed() {
<b class="fc">&nbsp;		return top.next == tail;</b>
&nbsp;	}
&nbsp;
&nbsp;	public String nodesToString(String separator) {
<b class="fc">&nbsp;		StringBuilder buf = new StringBuilder(size);</b>
<b class="fc">&nbsp;		for (E value : this) {</b>
<b class="fc">&nbsp;			buf.append(value).append(separator);</b>
<b class="fc">&nbsp;		}</b>
<b class="fc">&nbsp;		return buf.substring(0, buf.length() - separator.length());</b>
&nbsp;	}
&nbsp;	
&nbsp;	@Override
&nbsp;	public String toString() {
<b class="nc">&nbsp;		return this.nodesToString(&quot; &quot;);</b>
&nbsp;	}
&nbsp;	
&nbsp;	private void setCount(Node node) {
<b class="fc">&nbsp;		Node prev = node.prev;</b>
<b class="fc">&nbsp;		if (prev == head) {</b>
<b class="fc">&nbsp;			node.repeatCount = 1;</b>
&nbsp;		} else {
<b class="fc">&nbsp;			if (prev.value == node.value</b>
&nbsp;					|| node.value != null
<b class="fc">&nbsp;					&amp;&amp; node.value.equals(prev.value)) {</b>
<b class="fc">&nbsp;				node.repeatCount = prev.repeatCount + 1;</b>
&nbsp;			} else {
<b class="fc">&nbsp;				node.repeatCount = 1;</b>
&nbsp;			}
&nbsp;		}
&nbsp;	}
&nbsp;	
&nbsp;	@Override
&nbsp;	public Iterator&lt;E&gt; iterator() {
<b class="fc">&nbsp;		return new Itr();</b>
&nbsp;	}
&nbsp;	
&nbsp;	private class Itr implements Iterator&lt;E&gt; {
<b class="fc">&nbsp;        Node cursor = head.next;</b>
<b class="fc">&nbsp;        int expectedModCount = modCount;</b>
&nbsp;
<b class="fc">&nbsp;        Itr() {}</b>
&nbsp;
&nbsp;        public boolean hasNext() {
<b class="fc">&nbsp;            return cursor != tail;</b>
&nbsp;        }
&nbsp;
&nbsp;        @SuppressWarnings(&quot;unchecked&quot;)
&nbsp;        public E next() {
<b class="fc">&nbsp;            checkForComodification();</b>
<b class="fc">&nbsp;            if (!hasNext())</b>
<b class="nc">&nbsp;                throw new NoSuchElementException();</b>
<b class="fc">&nbsp;            E value = cursor.value;</b>
<b class="fc">&nbsp;            cursor = cursor.next;</b>
<b class="fc">&nbsp;            return value;</b>
&nbsp;        }
&nbsp;
&nbsp;        public void remove() {
<b class="nc">&nbsp;            checkForComodification();</b>
<b class="nc">&nbsp;            Stack.this.remove(cursor);</b>
<b class="nc">&nbsp;            expectedModCount = modCount;</b>
&nbsp;        }
&nbsp;
&nbsp;        @Override
&nbsp;        public void forEachRemaining(Consumer&lt;? super E&gt; action) {
<b class="nc">&nbsp;            Objects.requireNonNull(action);</b>
<b class="nc">&nbsp;            checkForComodification();</b>
<b class="nc">&nbsp;            for (; cursor != tail &amp;&amp; modCount == expectedModCount; cursor = cursor.next) {</b>
<b class="nc">&nbsp;            	E value = cursor.value;</b>
<b class="nc">&nbsp;            	action.accept(value);</b>
&nbsp;            }
<b class="nc">&nbsp;            checkForComodification();</b>
&nbsp;        }
&nbsp;
&nbsp;        final void checkForComodification() {
<b class="fc">&nbsp;            if (modCount != expectedModCount)</b>
<b class="nc">&nbsp;                throw new ConcurrentModificationException();</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;}
</code>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
      hljs.highlightAll()
      hljs.initLineNumbersOnLoad();
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2023-07-04 12:48</div>
</div>
</body>
</html>
